package org.jeecg.flowable.controller.definition;


import cn.hutool.db.PageResult;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.Tag;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.flowable.controller.definition.vo.model.*;
import org.jeecg.flowable.service.definition.BpmModelService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.io.IOException;

@RestController
@RequestMapping("/bpm/model")
@Validated
public class BpmModelController {

    @Resource
    private BpmModelService modelService;

    @GetMapping("/page")
    @AutoLog(value = "获得模型分页")
    @ApiOperation(value="获得模型分页", notes="获得模型分页")
    public CommonResult<PageResult<BpmModelPageItemRespVO>> getModelPage(BpmModelPageReqVO pageVO) {
        return success(modelService.getModelPage(pageVO));
    }

    @GetMapping("/get")
    @AutoLog(value = "获得模型")
    @ApiOperation(value="获得模型", notes="获得模型")
    public CommonResult<BpmModelRespVO> getModel(@RequestParam("id") String id) {
        BpmModelRespVO model = modelService.getModel(id);
        return success(model);
    }

    @PostMapping("/create")
    @AutoLog(value = "新建模型")
    @ApiOperation(value="新建模型", notes="新建模型")
    public CommonResult<String> createModel(@Valid @RequestBody BpmModelCreateReqVO createRetVO) {
        return success(modelService.createModel(createRetVO, null));
    }

    @PutMapping("/update")
    @AutoLog(value = "修改模型")
    @ApiOperation(value="修改模型", notes="修改模型")
    public CommonResult<Boolean> updateModel(@Valid @RequestBody BpmModelUpdateReqVO modelVO) {
        modelService.updateModel(modelVO);
        return success(true);
    }

    @PostMapping("/import")
    @AutoLog(value = "导入模型")
    @ApiOperation(value="导入模型", notes="导入模型")
    public CommonResult<String> importModel(@Valid BpmModeImportReqVO importReqVO) throws IOException {
        BpmModelCreateReqVO createReqVO = BpmModelConvert.INSTANCE.convert(importReqVO);
        // 读取文件
        String bpmnXml = IoUtils.readUtf8(importReqVO.getBpmnFile().getInputStream(), false);
        return success(modelService.createModel(createReqVO, bpmnXml));
    }

    @PostMapping("/deploy")
    @AutoLog(value = "部署模型")
    @ApiOperation(value="部署模型", notes="部署模型")
    public CommonResult<Boolean> deployModel(@RequestParam("id") String id) {
        modelService.deployModel(id);
        return success(true);
    }

    @PutMapping("/update-state")
    @AutoLog(value = "修改模型的状态")
    @ApiOperation(value="修改模型的状态", notes="实际更新的部署的流程定义的状态")
    public CommonResult<Boolean> updateModelState(@Valid @RequestBody BpmModelUpdateStateReqVO reqVO) {
        modelService.updateModelState(reqVO.getId(), reqVO.getState());
        return success(true);
    }

    @DeleteMapping("/delete")
    @AutoLog(value = "删除模型")
    @ApiOperation(value="删除模型", notes="删除模型")
    public CommonResult<Boolean> deleteModel(@RequestParam("id") String id) {
        modelService.deleteModel(id);
        return success(true);
    }
}
